#ifndef __GAMESCENE_H__
#define __GAMESCENE_H__
#include <cocos2d.h>
#include "../DataStructure/DataStruct.h"
#include "../Common/SingleTon.h"

USING_NS_CC;

#define REGIST_LAYER(name,class) AddGameLayer(#name,class::Create())
#define REGIST_SCENE_CREATE(class) class* Create() \
	{\
		class *pRet = new class();\
		if (pRet && pRet->init())\
		{\
			pRet->Init();\
			pRet->autorelease();\
			return pRet;\
		}\
		else\
		{\
			CC_SAFE_DELETE(pRet);\
			return 0;\
		}\
	}


class GameLayer;
class GameScene : public CCScene
{
public:
	REGIST_SCENE_CREATE(GameScene);
	void Init(){setAnchorPoint(ccp(0,0));OnInit();OnInitLayer();}
	virtual void OnInit() {}
	virtual void OnInitLayer() {}
	bool AddGameLayer(CSString classname);
	bool AddGameLayer(CSString classname,CSString layername);
	bool AddGameLayer(CSString layername,GameLayer* layer);
	void MTAddGameLayer(CSString classname);
	GameLayer* GetGameLayer(CSString name) 
	{
		CSHashMap<CSString,GameLayer*>::iterator iter=mGameLayerMap.Find(name);
		if(iter!=mGameLayerMap.End())
			return iter->second;
		else
			return 0;
	}
private:
	CSHashMap<CSString,GameLayer*> mGameLayerMap;
};

class GameSceneRegister
{
public:
	virtual void OnRegister()=0;
public:
	template<class T> void RegistScene(CSString name)
	{
		ObjCreatorBase* obj = (ObjCreatorBase*)(new ObjCreator<T>());
		mMap->Insert(name,obj);
	}
	void Register(CSHashMap<CSString,ObjCreatorBase*>* map )
	{
		mMap = map;
		OnRegister();
	}
private:
	CSHashMap<CSString,ObjCreatorBase*>* mMap;
};

class GameSceneMgr : public CSingleTon<GameSceneMgr>
{
public:
	GameSceneMgr():mNowScene(0) {}
	~GameSceneMgr() 
	{
		for(CSHashMap<CSString,ObjCreatorBase*>::iterator iter=mSceneMap.Begin();iter!=mSceneMap.End();++iter)
		{
			delete iter->second;
		}
	}
	void Init(GameSceneRegister& regist);
	void RunScene(CSString name);
	GameScene* GetNowScene() {return mNowScene;}
private:
	CSHashMap<CSString,ObjCreatorBase*> mSceneMap;
	GameScene* mNowScene;
};

#endif

